const goodModel = require('../model/good')
const cateModel = require('../model/cate')

class GoodController {
  // 描述：获取商品列表
  static async getGoodList(ctx) {
    let { page, size, cate, name } = ctx.request.query
    page = parseInt(page || 1)
    size = parseInt(size || 10)
    name = name || ''
    let params = {
      cate: cate || '',
      name: new RegExp(name, 'img')
    }
    if(!params.cate) delete params.cate
    const total = await goodModel.find(params).count()
    const list = await goodModel.find(params).limit(size).skip((page-1)*size)
    ctx.body = {
      err: 0,
      msg: 'success',
      data: { total, list }
    }
  }

  // 描述：获取商品品类列表
  static async getAllCates(ctx) {
    const list = await cateModel.find({})
    ctx.body = {
      err: 0,
      msg: 'success',
      data: { list }
    }
  }
}

module.exports = GoodController
